<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\Complain;
use think\Validate;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $category = Category::all(['status' => 'normal', 'pid' => 0]);
        $this->success('请求成功', compact('category'));
    }
    /**
     * @Author LK
     * @ApiTitle (分类列表)
     * @ApiMethod (get)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function category_list()
    {
        $param = request()->param();
        extract($param);
        $category = Category::all(['status' => 'normal', 'pid' => $id]);
        $this->success('请求成功', $category);
    }
    /**
     * @Author LK
     * @ApiTitle (投诉建议)
     * @ApiMethod (POST)
     * @ApiParams (name="sn", type="integer", required=true, sample="", description="订单号")
     * @ApiParams (name="description", type="integer", required=true, sample="", description="内容")
     * @ApiParams (name="images", type="integer", required=true, sample="", description="图片")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function complain()
    {
        $param = request()->post();
        extract($param);
        $validate = new Validate(
            [
                'sn' => 'require',
                'description' => 'require',
            ],
            [
                'sn.require' => '请输入订单号',
                'description.require' => '请输入投诉建议',
            ]
        );
        $result = $validate->check($param);
        if (!$result) {
            $this->error(__($validate->getError()));
        }
        $param['user_id'] = $this->auth->id;
        $model = new Complain($param);
        $res = $model->allowField(true)->save();
        if ($res) {
            $this->success('成功');
        }
        $this->error('失败');
    }
}
